package com.fenshi.manager.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fenshi.bean.UserBean;
import com.fenshi.manager.R;
import com.fenshi.manager.activity.order.OrderActivity;
import com.fenshi.manager.activity.user.UserCenter;
import com.fenshi.manager.adapter.MRecyclerAdapter;
import com.fenshi.presenter.MainPresenter;
import com.fenshi.presenter.imp.OrderListPresenterImp;
import com.fenshi.until.CookicUntil;
import com.fenshi.view.MainView;
import com.quickdv.activity.BaseActivity;
import com.quickdv.activity.adapter.viewholder.DataViewHolder;
import com.umeng.update.UmengUpdateAgent;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.hdodenhof.circleimageview.CircleImageView;
import zxing.activity.CaptureActivity;

/***
 *
 */
public class MainActivity extends BaseActivity implements View.OnClickListener, MainView, SwipeRefreshLayout.OnRefreshListener {

    CircleImageView head;
    MainPresenter presenter;
    MRecyclerAdapter adapter;
    View sms, order_manager, car_manager;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;

    @InjectView(R.id.main_recyclers)
    RecyclerView mainRecyclers;
    SwipeRefreshLayout orderRefreshlayout;
    @InjectView(R.id.user_name)
    TextView userName;
    @InjectView(R.id.user_phone)
    TextView userPhone;
    @InjectView(R.id.empty_view)
    TextView emptyView;
    private List<Map<String, Object>> data;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(false);
        getSupportActionBar().setIcon(R.mipmap.mjm);
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        initActionbar();
        return true;
    }


    @Override
    public int getLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        presenter.initDateEvent();

    }

    public void initUMeng() {
        UmengUpdateAgent.update(this);
        UmengUpdateAgent.setUpdateOnlyWifi(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.getOrder(true);
    }

    @Override
    public void initActionbar() {
        UmengUpdateAgent.update(this);
    }

    @Override
    public void initData() {
        presenter = new OrderListPresenterImp(this);
        data = new ArrayList<>();

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.friend_head:
                intent.setClass(MainActivity.this, UserCenter.class);
                startActivityForResult(intent, 200);
                return;
            case R.id.rent_sms:
                intent.setClass(MainActivity.this, CaptureActivity.class);
                break;
            case R.id.order_manager:
                intent.setClass(MainActivity.this, OrderActivity.class);
                break;
            case R.id.car_manager:
                intent.setClass(MainActivity.this, CarListManagerActivity.class);
                break;
        }
        startActivity(intent);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
        initUMeng();
    }

    @Override
    public void showLoad() {
        orderRefreshlayout.post(new Runnable() {
            @Override
            public void run() {
                orderRefreshlayout.setRefreshing(true);
            }
        });
    }

    @Override
    public void dismissLoad() {

        orderRefreshlayout.post(new Runnable() {
            @Override
            public void run() {
                orderRefreshlayout.setRefreshing(false);
            }
        });

    }

    @Override
    public void notifyDateChanged() {
        adapter.notifyDataSetChanged();


    }

    @Override
    public void setDate(List<Map<String, Object>> data, boolean isRefresh) {
        if (isRefresh)
            this.data.clear();
        if (data != null)
            this.data.addAll(data);

        if (this.data.size() == 0) {
            emptyView.setVisibility(View.VISIBLE);
            mainRecyclers.setVisibility(View.GONE);
        } else {
            emptyView.setVisibility(View.GONE);
            mainRecyclers.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }

    @Override
    public void initViewEvent(UserBean bean) {
        head = findView(R.id.friend_head);
        sms = findView(R.id.rent_sms);
        order_manager = findView(R.id.order_manager);
        car_manager = findView(R.id.car_manager);
        orderRefreshlayout = findView(R.id.order_refreshlayout);

        head.setOnClickListener(this);
        sms.setOnClickListener(this);
        order_manager.setOnClickListener(this);
        car_manager.setOnClickListener(this);
        orderRefreshlayout.setOnRefreshListener(this);

        setSupportActionBar(toolbar);
        orderType(R.layout.fragment_order_detail);

        userName.setText("你好,管理员" + bean.getRealName());
        userPhone.setText(bean.getUserName());
    }

    private void orderType(int layout) {
        adapter = new MRecyclerAdapter(this, data, layout, new String[]{"carimg", "carname", "username", "createtime"}, new int[]{R.id.park_car_pic, R.id.park_car_name, R.id.park_car_mileage_name, R.id.park_car_price}) {
            @Override
            public void addClickListener(DataViewHolder holder, final int position) {
                //添加具体点击实现
                holder.getRootView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        presenter.onItemClick(position);
                    }
                });
            }
        };

        mainRecyclers.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayout.VERTICAL);
        mainRecyclers.setLayoutManager(manager);

    }

    @Override
    public void onRefresh() {
        presenter.getOrder(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CookicUntil.setUser(new UserBean());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200 && resultCode == 200)
            finish();
        super.onActivityResult(requestCode, resultCode, data);
    }

    Long temp_time = Long.valueOf(0);

    @Override
    public void onBackPressed() {
        Date dt = new Date();
        Long time = dt.getTime();

        if (time - temp_time < 1000) {
            super.onBackPressed();
            return;
        } else {
            Toast.makeText(this, "再次点击退出", Toast.LENGTH_LONG).show();
            temp_time = time;
        }
    }
}
